package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CostumExcepition;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.service.FileStorageService;
import com.heima.utils.common.MyThreadLocal;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper,WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;


    //上传素材
    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {

        if(multipartFile.getSize()==0||multipartFile==null){
            throw new CostumExcepition(AppHttpCodeEnum.PARAM_INVALID);
        }
        //生成文件名字
        String name = UUID.randomUUID().toString().replace("-", "");
        //文件后缀
        String filename = multipartFile.getOriginalFilename();
        String sub = filename.substring(filename.lastIndexOf("."));
        filename=name+sub;

        String fileUrl = fileStorageService.uploadImgFile("", filename, multipartFile.getInputStream());

        WmMaterial material = new WmMaterial();
        material.setCreatedTime(new Date());
        material.setType((short)0);
        material.setUrl(fileUrl);
        material.setUserId(MyThreadLocal.getUser().getId());
        material.setIsCollection((short)0);
        save(material);

        return ResponseResult.okResult(material);

    }

    //查询素材
    @Override
    public ResponseResult getList(WmMaterialDto wmMaterialDto) {

        wmMaterialDto.checkParam();

        Page<WmMaterial> page = this.page(new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize())
                , Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getIsCollection, wmMaterialDto.getIsCollection())
                        .eq(WmMaterial::getUserId, MyThreadLocal.getUser().getId())
                        .orderByDesc(WmMaterial::getCreatedTime));

        PageResponseResult result = new PageResponseResult();

        result.setTotal((int)page.getTotal());
        result.setData(page.getRecords());
        result.setSize((int)page.getSize());
        result.setCurrentPage((int)page.getCurrent());




        return result;
    }
}
